//
//  YDHomePageController.m
//  YDTestDrive
//
//  Created by ios on 2017/2/9.
//  Copyright © 2017年 ios. All rights reserved.
//

#import "YDHomePageController.h"
#import "YDAddTryDriveController.h"
#import "ViewController.h"

#import "YDHomePageCell.h"
#import "YDNoDataView.h"

#import "YDPassengerModel.h"

#import "AFNetworking.h"

@interface YDHomePageController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *passangerListArray;
@property (nonatomic, strong) YDNoDataView *noDataView;

@end

@implementation YDHomePageController

- (instancetype)init
{
    if (self = [super init]) {
        _passangerListArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"客流列表";

    [self createView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self loadPassengerListFormNetwork];
}


- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64.0f - 49.0f) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib(@"YDHomePageCell")];
    _tableView.backgroundColor = kViewControllerBackgroundColor;
    [self.view addSubview:_tableView];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadPassengerListFormNetwork];
    }];
    
    _noDataView = [YDNoDataView noDataView];
    _noDataView.frame = _tableView.bounds;
    _noDataView.hidden = YES;
    [_tableView addSubview:_noDataView];
    [_tableView bringSubviewToFront:_noDataView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 48.0f, 24.0f);
    button.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [button setTitle:@"+试驾" forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"home_btn_new_normal"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"home_btn_new_click"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(rightAddTryDriveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)rightAddTryDriveButtonClick
{
    YDAddTryDriveController *VC = [[YDAddTryDriveController alloc] initWithNibName:@"YDAddTryDriveController" bundle:nil];
    [self.navigationController pushViewController:VC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.passangerListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDPassengerModel *model = self.passangerListArray[indexPath.row];
    YDHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YDHomePageCell"];
    cell.passangerModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 74.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YDPassengerModel *model = [self.passangerListArray objectAtIndex:indexPath.row];
    YDAddTryDriveController *VC = [[YDAddTryDriveController alloc] initWithNibName:@"YDAddTryDriveController" bundle:nil];
    VC.passengerModel = model;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 网络
#pragma mark 客流列表
- (void)loadPassengerListFormNetwork
{
    NSString *url = [NSString stringWithFormat:@"%@sid=%@", kPassengerListAddress, getNSUser(kAPP_SID)];
    NSDictionary *dic = @{};
    
    //[MBProgressHUD showStatus:@"" toView:self.view];
    
    
    [YDDataService POST:dic url:url block:^(id result) {
        
        if ([[result valueForKey:@"code"] isEqualToString:@"S_OK"]) {
            NSArray *passangerArray = [result valueForKey:@"var"];
            [self updateTableViewWithPassangerList:passangerArray];
        } else {
            [self isNeedAgainLoginWithErrorCode:[result valueForKey:@"code"]];
        }
        
        [self.tableView.mj_header endRefreshing];
        
    } failBlock:^(id error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)updateTableViewWithPassangerList:(NSArray *)passangerArray
{
    [self.passangerListArray removeAllObjects];
    
    for (NSDictionary *dic in passangerArray) {
        YDPassengerModel *model = [YDPassengerModel instanceWithDict:dic];
        [self.passangerListArray addObject:model];
    }
    [self.tableView reloadData];
    
    if (self.passangerListArray.count > 0) {
        self.noDataView.hidden = YES;
    } else {
        self.noDataView.hidden = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
